package com.sh.util;

import com.sh.wy.system.entity.SysMenu;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Auther: 世豪学java
 * @Date: 2023/12/19 - 12 - 19 - 20:14
 * @Decsription: com.sh.util
 * @version: 1.0
 */
//生成树工具类
public class MakeMenuTree {
    public static List<SysMenu> makeTree(List<SysMenu> menuList,Integer pid){
        List<SysMenu> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                //过滤出除了父菜单剩下的菜单
                .filter(item -> item != null && item.getParentId() == pid)
                //遍历这些菜单
                .forEach(dom -> {
                    //将这些菜单存到新的menu的对象中
                    SysMenu menu = new SysMenu();
                    BeanUtils.copyProperties(dom,menu);
                    //递归调用
                    List<SysMenu> menus = makeTree(menuList, dom.getMenuId());
                    menu.setChildren(menus);
                    list.add(menu);
                });
        return list;

        /*
        List<SysMenu> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getParentId() == pid)
                .forEach(dom -> {
                    SysMenu menu = new SysMenu();
                    BeanUtils.copyProperties(dom, menu);
                    List<SysMenu> menus = makeTree(menuList, dom.getMenuId());
                    menu.setChildren(menus);
                    list.add(menu);
                });
        return list;
        */
    }
}
